//
//  ProcessViewController.m
//  OASystem
//
//  Created by zhoupengfei on 15/11/1.
//  Copyright © 2015年 zpf. All rights reserved.
//

#import "ProcessViewController.h"
#import "HQTableViewController.h"
#import "OAHistroyViewController.h"
#import "MBProgressHUD.h"
#import "HttpTools.h"
#import "Toast+UIView.h"
@interface ProcessViewController ()
@property(nonatomic,strong)HQTableViewController * hqVC;

@end

@implementation ProcessViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    self.hqVC =(HQTableViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"HQVCID"];
    self.hqVC.fileId = self.fileId;
    self.hqVC.formType = self.formType;
    self.hqVC.view.frame = CGRectMake(0, 0, self.view.bounds.size.width, self.view.bounds.size.height - 44);
    [self.view addSubview:self.hqVC.view];
    [self addChildViewController:self.hqVC];
    
    UIBarButtonItem * rightItem = [[UIBarButtonItem alloc]initWithTitle:@"流转历史" style:UIBarButtonItemStylePlain target:self action:@selector(historyAction)];
    self.navigationItem.rightBarButtonItem = rightItem;

    if (self.isUnDoWork) {
        [self.operationBtn setTitle:@"签收" forState:UIControlStateNormal];
    }else{
        [self.operationBtn setTitle:@"编辑" forState:UIControlStateNormal];
    }
    
}


#pragma mark 签收/编辑
- (IBAction)operationAction:(UIButton *)sender {
    UIButton * btn = (UIButton*)sender;
    if (self.isUnDoWork && [btn.currentTitle isEqualToString:@"签收"]) {//从待办工作路径过来 先调用 签收 接口
        [self setupSign]; //签收
    }else{
        if ([btn.currentTitle isEqualToString:@"编辑"]) { //开始编辑
            [btn setTitle:@"取消" forState:UIControlStateNormal];
            self.saveBtn.enabled = YES;
            self.sendBtn.enabled = YES;
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBeginEdit" object:nil userInfo:@{@"edit" : @"Y"}];
        }else{
            [btn setTitle:@"编辑" forState:UIControlStateNormal]; //取消编辑
            self.saveBtn.enabled = NO;
            self.sendBtn.enabled = NO;
            
            [[NSNotificationCenter defaultCenter] postNotificationName:@"kBeginEdit" object:nil userInfo:@{@"edit" : @"N"}];
        }
    }
    
}

#pragma mark 签收
-(void)setupSign{
    [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    NSString * userId = [[NSUserDefaults standardUserDefaults]objectForKey:@"userId"];
    NSArray * array = @[@{@"unId":self.fileId},@{@"userId":userId}];
    [HttpTools postWithURL:KexchdataURL arrays:array method:KSetSignInMethod success:^(id JSON) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
        [self.operationBtn setTitle:@"编辑" forState:UIControlStateNormal];
        [self.view makeToast:@"签收成功"];
        [self operationAction:self.operationBtn];
        [[NSNotificationCenter defaultCenter] postNotificationName:@"refreshOADetailData" object:nil];
    } failure:^(NSError *error) {
        [MBProgressHUD hideAllHUDsForView:self.view animated:YES];
    }];
    
    
}
- (IBAction)saveAction:(UIButton *)sender {
    
    [[NSNotificationCenter defaultCenter] postNotificationName:@"kSave" object:nil];
    
}

- (IBAction)sendAction:(UIButton *)sender{

      [[NSNotificationCenter defaultCenter] postNotificationName:@"kSend" object:nil];
    
}

#pragma mark 流转历史
-(void)historyAction{
    OAHistroyViewController* historyVC = (OAHistroyViewController*)[self.storyboard instantiateViewControllerWithIdentifier:@"HistoryVC"];
    historyVC.fileId = self.fileId;
    [self.navigationController pushViewController:historyVC animated:YES];
}
@end
